package com.mingqijia.gassafety.console.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gap.basic.base.CommonResponse;
import com.mingqijia.gassafety.db.entity.ValveInfo;
import com.mingqijia.gassafety.db.entity.vo.ValveInfoListVO;
import com.mingqijia.gassafety.shared.constant.Constants;
import com.mingqijia.gassafety.shared.constant.OperateRecordEnum;
import com.mingqijia.gassafety.shared.dto.LogVO;
import com.mingqijia.gassafety.shared.log.LogRecord;
import com.mingqijia.gassafety.shared.utils.commonUtils.PageUtil;
import com.mingqijia.gassafety.webserver.request.ValveInfoAddRequest;
import com.mingqijia.gassafety.webserver.request.ValveInfoRequest;
import com.mingqijia.gassafety.webserver.service.ValveInfoService;
import com.mingqijia.gassafety.webserver.utils.LogRecordAspect;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 电磁阀信息表 前端控制器
 * </p>
 *
 * @author guoliang.lao
 * @since 2023-08-22
 */
@Slf4j
@Api(tags = "电磁阀信息管理")
@RestController(value = "ValveInfoController")
@RequestMapping("/business/v1/valveInfo")
public class ValveInfoController {

    @Autowired
    ValveInfoService valveInfoService;

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiOperation("电磁阀信息列表")
    public CommonResponse<PageUtil<ValveInfoListVO>> getList(@RequestBody ValveInfoRequest req){
        return CommonResponse.success(valveInfoService.getList(req));
    }

    @LogRecord(operateType = OperateRecordEnum.OPERATE_TYPE_30)
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation("新增电磁阀信息")
    public CommonResponse<String> add(@RequestBody @Valid ValveInfoAddRequest req){
        log.info("ValveInfoController add req："+req);
        valveInfoService.add(req);
        return CommonResponse.success("");
    }

    @LogRecord(operateType = OperateRecordEnum.OPERATE_TYPE_32)
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    @ApiOperation("删除电磁阀信息")
    public CommonResponse<String> remove(@RequestBody @Valid ValveInfoAddRequest req){
        log.info("ValveInfoController remove req："+req.getId());
        LambdaQueryWrapper<ValveInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ValveInfo::getIsDeleted, Constants.IS_DELETED_FALSE).eq(ValveInfo::getId,req.getId());
        ValveInfo valveInfo = valveInfoService.getOne(wrapper);
        if (valveInfo == null) return CommonResponse.failure("删除的数据不存在！");

        //企业级操作日志
        LogVO vo = new LogVO();
        vo.setDetail("【电磁阀名称："+ valveInfo.getValveName()+"】信息");
        vo.setCommonInfo(valveInfo.getValveName());
        LogRecordAspect.threadLocal.set(vo);

        valveInfoService.remove(req.getId());
        return CommonResponse.success("");
    }

    @LogRecord(operateType = OperateRecordEnum.OPERATE_TYPE_31)
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation("编辑电磁阀信息")
    public CommonResponse<String> edit(@RequestBody @Valid ValveInfoAddRequest req){
        log.info("ValveInfoController edit req："+req);
        valveInfoService.edit(req);
        return CommonResponse.success("");
    }

    @RequestMapping(value = "/modelList", method = RequestMethod.POST)
    @ApiOperation("电磁阀信息型号列表")
    public CommonResponse<List<String>> getModelList(){
        return CommonResponse.success(valveInfoService.getModelList());
    }

}

